from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
from alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequest

# 公钥 修改
alipay_public_key_string = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkK8/ZjOLCoQWKYKNmakT7v9YaEzOzdgWU7PynqAr0hOuhLBktfZZR0LYDHP350WShziH/rxhOeXgXTAeZ/88X9GzzLvLu0V9HSQHneiLpMqAUP0hnOygk8UDHz7kRtVWEL4FJzUA2DmxkacgaaRTRuMOaUUQX4EJcGMrkAKuPJrfqmEsVNHsrVckT3RmHZeID2e/9VnmfivO0F/4/UQmPyaDI1gaBrNevbPbiZ+WIX94/IPlGvyGdD2A2W+Teaj8U9Zj0XWnOqueqGGkDtmt1sIYAku/SBpKqnFxkHqNnzfN710nF6O3M6+sjrKJ6cxbzN6CXbWmyv72tkDzIlZOCQIDAQAB
-----END PUBLIC KEY-----"""
# 私钥 修改
app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA2TBqNni9wN+azO2pelF8oCWMVV2EpXW0bk1apewzqj7yqzBf
CMmDwkQlufimRzjZ5Z6PWK4CN2ZqpKq+pv7qPW045bd+LhT9W04og705ZMdWWzOA
mmJXOr+AcseoxZzLM5Et1ibAZvHSIyOo65KjudySYpmqY6R1k8zD6Vl5byW/+dkR
N0vegXZFqxOEipwaooRTboV2xNsLuZ94uS6gajDBaT4MLOJTWtd8QQNCt3IxNfRE
VcMF/2XlxIMGqp3tQQHF3FuqN8hDXMi5J685clJ9TyxbK8giF3j0UMM9F224JEzg
GusjOtciPjPekcK1aL/AGSOYkBTinIKZ5BRImwIDAQABAoIBABmATMhOjwEpgBSV
xzN8g+SN5BdV3rBKzL+yx3T58esIbxfYmx/+AUF7HO7kGin8q+EBK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qRUWxrNGB7B2X2kvFqlM+61W81yerQ
HQaTHtRwzpDhYEJ60FfQKmRDTT+UaNXSB1Z9ZFVjTxNsc2shD/JIIe4=
-----END RSA PRIVATE KEY-----"""
# 实例化客户端
alipay_client_config = AlipayClientConfig()
alipay_client_config.server_url = 'https://openapi-sandbox.dl.alipaydev.com/gateway.do'
# 修改
alipay_client_config.app_id = '9021000122680272'
alipay_client_config.app_private_key = app_private_key_string
alipay_client_config.alipay_public_key = alipay_public_key_string
client = DefaultAlipayClient(alipay_client_config=alipay_client_config)


# 这段代码定义了一个函数 get_pay，用于获取支付宝支付链接。函数接受三个参数：商户订单号 out_trade_no、支付金额 total_amount 和支付完成后的回调URL
# return_url。函数内部通过构造请求参数对象，并使用支付宝SDK执行API调用，最后返回支付宝支付链接。
def get_pay(out_trade_no, total_amount, return_url):
    """
    获取支付宝支付链接
    :param out_trade_no: 商户订单号
    :param total_amount: 支付金额
    :param return_url: 支付完成后的回调URL
    :return: 支付宝支付链接
    """
    # 构造请求参数对象
    model = AlipayTradePagePayModel()
    model.out_trade_no = out_trade_no
    model.total_amount = str(total_amount)
    model.subject = "测试"
    model.body = "支付宝测试"
    model.product_code = "FAST_INSTANT_TRADE_PAY"
    request = AlipayTradePagePayRequest(biz_model=model)
    request.notify_url = return_url + '?t=' + out_trade_no
    request.return_url = return_url + '?t=' + out_trade_no
    # 执行API调用
    response = client.page_execute(request, http_method="GET")
    return response
